﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using System.IO;

namespace EnterpriseFund.Controllers
{

    public class Key_NSRXXController : AdminBaseController
    {
        //
        // GET: /Key_NSRXX/

        public ActionResult Index(string report, string NSRMC, string NSRBM, string HY_DM, string FEnterprisePropNo, string FFinancePropNo, string IsNoFull)
        {

            string strwhere = "1=1";
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
                ViewData["NSRMC"] = NSRMC;
            }
            if (NSRBM != null && NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
                ViewData["NSRBM"] = NSRBM;
            }
            //if (HY_DM != null && HY_DM != "" && HY_DM != "null")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //    ViewData["HY_DM"] = HY_DM;
            //}
            if (FEnterprisePropNo != null && FEnterprisePropNo != "" && FEnterprisePropNo != "null")
            {
                strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
                ViewData["FEnterprisePropNo"] = FEnterprisePropNo;
            }
            if (FFinancePropNo != null && FFinancePropNo != "" && FFinancePropNo != "null")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
                ViewData["FFinancePropNo"] = FFinancePropNo;
            }
            //if (IsNoFull != null && IsNoFull != "")
            //{
            //    if (IsNoFull == "Y")
            //    {
            strwhere += " and  NSRDZDAH  in (select NSRDZDAH from fund.t_Key_NSRXX where FReportName='" + report + "')";
            //    }
            //    ViewData["IsNoFull"] = IsNoFull;
            //}

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            int pageIndex = 1;
            ViewData["rpt"] = report;
            ViewData["strwhere"] = System.Web.HttpUtility.UrlEncode(strwhere, System.Text.Encoding.GetEncoding("utf-8"));
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            List<Model.DJ_NSRXX> DJ_NSRXXList = new List<Model.DJ_NSRXX>();
            DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex);
            return View(DJ_NSRXXList);
        }

        public ActionResult TaxPayerManagerListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string NSRMC = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string HY_DM = Request.Params["HY_DM"];
            string FEnterprisePropNo = Request.Params["FEnterprisePropNo"];
            string FFinancePropNo = Request.Params["FFinancePropNo"];
            string IsNoFull = Request.Params["IsNoFull"];
            string destrpt = Request.Params["RPT"];
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "1=1";

            if (NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }
            if (NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }
            //if (HY_DM != "")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //}
            if (FEnterprisePropNo != "")
            {
                strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
            }
            if (FFinancePropNo != "")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
            }
            //if (IsNoFull != "")
            //{
            //    if (IsNoFull == "Y")
            //    {
            //        strwhere += " and  (FFinancePropNo is null or FEnterprisePropNo is null)";
            strwhere += " and  NSRDZDAH  in (select NSRDZDAH from fund.t_Key_NSRXX where FReportName='" + destrpt + "')";
            //    }
            //}
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            List<Model.DJ_NSRXX> DJ_NSRXXList = new List<Model.DJ_NSRXX>();
            DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex);
            return PartialView("TaxPayerManagerListPartial", DJ_NSRXXList);
        }
        public ActionResult ManagerListPartial()
        {

            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string NSRMC = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string HY_DM = Request.Params["HY_DM"];
            string FEnterprisePropNo = Request.Params["FEnterprisePropNo"];
            string FFinancePropNo = Request.Params["FFinancePropNo"];
            string IsNoFull = Request.Params["IsNoFull"];
            string destrpt = Request.Params["RPT"];
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "1=1";

            if (NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }
            if (NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }
            //if (HY_DM != "")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //}
            if (FEnterprisePropNo != "")
            {
                strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
            }
            if (FFinancePropNo != "")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
            }
            //if (IsNoFull != "")
            //{
            //    if (IsNoFull == "Y")
            //    {
            //        strwhere += " and  (FFinancePropNo is null or FEnterprisePropNo is null)";
            strwhere += " and  NSRDZDAH not in (select NSRDZDAH from fund.t_Key_NSRXX where FReportName='" + destrpt + "')";
            //    }
            //}
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            List<Model.DJ_NSRXX> DJ_NSRXXList = new List<Model.DJ_NSRXX>();
            DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex);
            return PartialView("ManagerListPartial", DJ_NSRXXList);
        }
        public ActionResult PropALLEdit(string noList, string pageNoList, string strwhere)
        {

            ViewData["noList"] = noList;
            ViewData["pageNoList"] = pageNoList;
            ViewData["strwhere"] = strwhere;

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            return View();
        }

        public string ExistsProp(string noList, string type)
        {
            try
            {
                string selectno = "";
                BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
                if (type != "3")
                {
                    string[] nolists = noList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string no in nolists)
                    {
                        if (selectno != "")
                        {
                            selectno += "," + no;
                        }
                        else
                        {
                            selectno += no;
                        }
                    }
                }
                else
                {
                    selectno = noList;
                }
                string year = OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
                BLL.Common CommonBLL = new BLL.Common();
                string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
                string strwhere1 = "";
                if (str != "")
                {
                    strwhere1 = @"CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
 NOT in
(SELECT  CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
         FROM fund.V_SB_ZSXX WHERE" + str + @") and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                }
                else
                {
                    strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                }

                if (DJ_NSRXXBLL.Exists(decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), selectno, type, strwhere1))
                {
                    return "1";
                }
                else
                {
                    return "2";
                }
            }
            catch
            {
                return "3";
            }
        }
        public ActionResult PropAdd(string report, string NSRMC, string NSRBM, string HY_DM, string FEnterprisePropNo, string FFinancePropNo, string IsNoFull)
        {

            string strwhere = "1=1";
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
                ViewData["NSRMC"] = NSRMC;
            }
            if (NSRBM != null && NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
                ViewData["NSRBM"] = NSRBM;
            }
            //if (HY_DM != null && HY_DM != "" && HY_DM != "null")
            //{
            //    strwhere += " and HY_DM='" + HY_DM + "'";
            //    ViewData["HY_DM"] = HY_DM;
            //}
            if (FEnterprisePropNo != null && FEnterprisePropNo != "" && FEnterprisePropNo != "null")
            {
                strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
                ViewData["FEnterprisePropNo"] = FEnterprisePropNo;
            }
            if (FFinancePropNo != null && FFinancePropNo != "" && FFinancePropNo != "null")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
                ViewData["FFinancePropNo"] = FFinancePropNo;
            }
            //if (IsNoFull != null && IsNoFull != "")
            //{
            //    if (IsNoFull == "Y")
            //    {
            strwhere += " and  NSRDZDAH not in (select NSRDZDAH from fund.t_Key_NSRXX where FReportName='" + report + "')";
            //    }
            //    ViewData["IsNoFull"] = IsNoFull;
            //}

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            int pageIndex = 1;
            ViewData["rpt"] = report;
            ViewData["strwhere"] = System.Web.HttpUtility.UrlEncode(strwhere, System.Text.Encoding.GetEncoding("utf-8"));
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            List<Model.DJ_NSRXX> DJ_NSRXXList = new List<Model.DJ_NSRXX>();
            DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex);
            return View(DJ_NSRXXList);
            //  return View();
        }

        public ActionResult PropALLSave()
        {
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            string type = Fetch.Post("type");
            string noList = Fetch.Post("hidnoList");
            string pageNoList = Fetch.Post("hidpageNoList");
            string strwhere = Fetch.Post("hidstrwhere");
            string fEnterprisePropNo = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FEnterprisePropNo"); //Fetch.Post("FEnterprisePropNo");
            string fFinancePropNo = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FFinancePropNo"); //Fetch.Post("FFinancePropNo");

            try
            {
                StringBuilder selectNo = new StringBuilder();
                //string selectNo = "";
                List<Model.DJ_NSRXX_Extension1> DJ_NSRXX_Extension1List = new List<Model.DJ_NSRXX_Extension1>();
                switch (type)
                {
                    case "0":
                        string[] nolists = noList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string no in nolists)
                        {
                            if (selectNo.ToString() != "")
                            {
                                selectNo.Append("," + no);
                            }
                            else
                            {
                                selectNo.Append(no);
                            }
                            Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                            model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                            model.FEntityNo = GlobalConstant.ENTITYNO;
                            model.FEnterprisePropNo = fEnterprisePropNo;
                            model.FFinancePropNo = fFinancePropNo;
                            model.FInsDate = DateTime.Now;
                            model.NSRDZDAH = decimal.Parse(no);
                            model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            model.FInsIP = GenerateUtil.GetClientIPv4Address();
                            DJ_NSRXX_Extension1List.Add(model);

                        }
                        DJ_NSRXXBLL.AddALL(DJ_NSRXX_Extension1List, selectNo.ToString());
                        break;
                    case "1":
                        string[] pagenolists = pageNoList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string no in pagenolists)
                        {
                            if (selectNo.ToString() != "")
                            {
                                selectNo.Append("," + no);
                            }
                            else
                            {
                                selectNo.Append(no);
                            }
                            Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                            model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                            model.FEntityNo = GlobalConstant.ENTITYNO;
                            model.FEnterprisePropNo = fEnterprisePropNo;
                            model.FFinancePropNo = fFinancePropNo;
                            model.FInsDate = DateTime.Now;
                            model.NSRDZDAH = decimal.Parse(no);
                            model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            model.FInsIP = GenerateUtil.GetClientIPv4Address();
                            DJ_NSRXX_Extension1List.Add(model);
                        }
                        DJ_NSRXXBLL.AddALL(DJ_NSRXX_Extension1List, selectNo.ToString());
                        break;
                    case "2":
                        //List<string> noStrList = DJ_NSRXXBLL.GetDzdah(OperateSession.get(GlobalConstant.PARAM_SYSYEAR), strwhere);
                        //foreach (string no in noStrList)
                        //{
                        //    if (selectNo.ToString() != "")
                        //    {
                        //        selectNo.Append("," + no);
                        //    }
                        //    else
                        //    {
                        //        selectNo.Append(no);
                        //    }
                        //    Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                        //    model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                        //    model.FEntityNo = GlobalConstant.ENTITYNO;
                        //    model.FEnterprisePropNo = fEnterprisePropNo;
                        //    model.FFinancePropNo = fFinancePropNo;
                        //    model.FInsDate = DateTime.Now;
                        //    model.NSRDZDAH = decimal.Parse(no);
                        //    model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                        //    model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                        //    model.FInsIP = GenerateUtil.GetClientIPv4Address();
                        //    DJ_NSRXX_Extension1List.Add(model);
                        //}

                        Model.DJ_NSRXX_Extension1 modelz = new Model.DJ_NSRXX_Extension1();
                        modelz.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                        modelz.FEntityNo = GlobalConstant.ENTITYNO;
                        modelz.FEnterprisePropNo = fEnterprisePropNo;
                        modelz.FFinancePropNo = fFinancePropNo;
                        modelz.FInsDate = DateTime.Now;
                        modelz.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                        modelz.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                        modelz.FInsIP = GenerateUtil.GetClientIPv4Address();
                        string year = OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
                        BLL.Common CommonBLL = new BLL.Common();
                        string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
                        string strwhere1 = "";
                        if (str != "")
                        {
                            strwhere1 = @"CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
 NOT in
(SELECT  CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
         FROM fund.V_SB_ZSXX WHERE" + str + @") and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                        }
                        else
                        {
                            strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                        }

                        DJ_NSRXXBLL.AddALLPre(strwhere, OperateSession.get(GlobalConstant.PARAM_SYSYEAR), modelz, strwhere1);
                        break;
                }

                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }
            ViewData["noList"] = noList;
            ViewData["pageNoList"] = pageNoList;
            ViewData["strwhere"] = strwhere;

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            return View("PropALLEdit");
        }

        public ActionResult PropEdit(string NSRDZDAH)
        {
            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            ViewData["NSRDZDAH"] = NSRDZDAH;
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            Model.DJ_NSRXX DJ_NSRXXModel = new Model.DJ_NSRXX();
            DJ_NSRXXModel = DJ_NSRXXBLL.GetModel(decimal.Parse(NSRDZDAH), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return View(DJ_NSRXXModel);
        }

        public ActionResult PropSave(Model.DJ_NSRXX model)
        {
            string NSRDZDAH = Fetch.Post("hidNSRDZDAH");
            BLL.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1BLL = new BLL.DJ_NSRXX_Extension1();
            BLL.DJ_NSRXX_Extension2 DJ_NSRXX_Extension2BLL = new BLL.DJ_NSRXX_Extension2();
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            try
            {
                Model.DJ_NSRXX_Extension1 model1 = DJ_NSRXX_Extension1BLL.GetModel(decimal.Parse(NSRDZDAH), decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), GlobalConstant.ENTITYNO);
                Model.DJ_NSRXX_Extension2 model2 = DJ_NSRXX_Extension2BLL.GetModel(decimal.Parse(NSRDZDAH), GlobalConstant.ENTITYNO);
                Model.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1Model = new Model.DJ_NSRXX_Extension1();
                Model.DJ_NSRXX_Extension2 DJ_NSRXX_Extension2Model = new Model.DJ_NSRXX_Extension2();
                if (model1 != null)
                {
                    DJ_NSRXX_Extension1Model.FInsDate = model1.FInsDate;
                    DJ_NSRXX_Extension1Model.FInsDeptNo = model1.FInsDeptNo;
                    DJ_NSRXX_Extension1Model.FInsEmpNo = model1.FInsEmpNo;
                    DJ_NSRXX_Extension1Model.FInsIP = model1.FInsIP;
                    DJ_NSRXX_Extension1Model.FUpdDate = DateTime.Now;
                    DJ_NSRXX_Extension1Model.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension1Model.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension1Model.FUpdIP = GenerateUtil.GetClientIPv4Address();
                }
                else
                {
                    DJ_NSRXX_Extension1Model.FInsDate = DateTime.Now;
                    DJ_NSRXX_Extension1Model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension1Model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension1Model.FInsIP = GenerateUtil.GetClientIPv4Address();
                }

                if (model2 != null)
                {
                    DJ_NSRXX_Extension2Model.FInsDate = model2.FInsDate;
                    DJ_NSRXX_Extension2Model.FInsDeptNo = model2.FInsDeptNo;
                    DJ_NSRXX_Extension2Model.FInsEmpNo = model2.FInsEmpNo;
                    DJ_NSRXX_Extension2Model.FInsIP = model2.FInsIP;
                    DJ_NSRXX_Extension2Model.FUpdDate = DateTime.Now;
                    DJ_NSRXX_Extension2Model.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension2Model.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension2Model.FUpdIP = GenerateUtil.GetClientIPv4Address();
                }
                else
                {
                    DJ_NSRXX_Extension2Model.FInsDate = DateTime.Now;
                    DJ_NSRXX_Extension2Model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension2Model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension2Model.FInsIP = GenerateUtil.GetClientIPv4Address();
                }

                DJ_NSRXX_Extension2Model.GS_NSRMC = model.GS_NSRMC;
                DJ_NSRXX_Extension2Model.GS_NSRSBH = model.GS_NSRSBH;
                DJ_NSRXX_Extension2Model.FEntityNo = GlobalConstant.ENTITYNO;
                DJ_NSRXX_Extension1Model.FEnterprisePropNo = model.FEnterprisePropNo;
                DJ_NSRXX_Extension1Model.FFinancePropNo = model.FFinancePropNo;
                DJ_NSRXX_Extension1Model.FEntityNo = GlobalConstant.ENTITYNO;

                DJ_NSRXX_Extension1Model.NSRDZDAH = decimal.Parse(NSRDZDAH);
                DJ_NSRXX_Extension2Model.NSRDZDAH = decimal.Parse(NSRDZDAH);

                DJ_NSRXX_Extension1Model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));

                DJ_NSRXXBLL.Add(DJ_NSRXX_Extension1Model, DJ_NSRXX_Extension2Model);

                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }
            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();
            ViewData["NSRDZDAH"] = NSRDZDAH;
            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            Model.DJ_NSRXX DJ_NSRXXModel = new Model.DJ_NSRXX();
            DJ_NSRXXModel = DJ_NSRXXBLL.GetModel(decimal.Parse(NSRDZDAH), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return View("PropEdit", DJ_NSRXXModel);
        }

        /// <summary>
        /// 获取下拉框的值
        /// </summary>
        /// <param name="fEnterprisePropNo"></param>
        /// <returns></returns>
        public string GetList(string fEnterprisePropNo)
        {
            BLL.FinanceProp financePropBLL = new BLL.FinanceProp();
            List<Model.FinanceProp> financePropList = financePropBLL.GetSelectList(fEnterprisePropNo, OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return JsonConvert.SerializeObject(financePropList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.DJ_NSRXX> GetDJ_NSRXXListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            string fiedls = "NSRDZDAH";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.[fn_DJ_NSRXX](" + OperateSession.get(GlobalConstant.PARAM_SYSYEAR) + ")", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "NSRDZDAH", out total);
            List<Model.DJ_NSRXX> DJ_NSRXXList = DJ_NSRXXBLL.DataTableToList(ds.Tables[0]);
            List<Model.DJ_NSRXX> DJ_NSRXXAllList = new List<Model.DJ_NSRXX>();
            total = DJ_NSRXXBLL.GetRecordCount(strWhere, OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                DJ_NSRXXAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (DJ_NSRXXList.Count > i)
                {
                    DJ_NSRXXAllList[((pageIndex - 1) * pageRowSize) + i] = DJ_NSRXXList[i];
                }
            }

            return DJ_NSRXXAllList;
        }

        //public string SaveAdd(string NSRDZDAH)
        //{
        //    BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
        //    Model.t_Key_NSRXX codeNameObj = new Model.t_Key_NSRXX();
        //    string[] str = NSRDZDAH.Split(',');

        //    try
        //    {
        //        for (int i = 0; i < str.Length - 1; i++)
        //        {
        //            codeNameObj.NSRDZDAH = Convert.ToDecimal(str[i]);
        //            codeNameBll.Add(codeNameObj);
        //        }
        //        return "ok";
        //    }
        //    catch (Exception e)
        //    {
        //        return "error";
        //    }
        //}


        public string SaveAdd(string NSRDZDAH, string Report)
        {
            BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
            Model.t_Key_NSRXX codeNameObj = new Model.t_Key_NSRXX();
            List<EnterpriseFund.Model.t_Key_NSRXX> modellist = new List<EnterpriseFund.Model.t_Key_NSRXX>();

            string[] str = NSRDZDAH.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                for (int i = 0; i < str.Length; i++)
                {
                   codeNameObj=  new Model.t_Key_NSRXX();
                    codeNameObj.NSRDZDAH = Convert.ToDecimal(str[i]);
                    codeNameObj.FReportName = Report;
                   // codeNameBll.Add(codeNameObj);
                    modellist.Add(codeNameObj);
                }
                codeNameBll.AddList(modellist);
                return "ok";
            }
            catch (Exception e)
            {
                return "error";
            }
        }
        public string btnAddAll(string Report)
        {
            try
            {
                BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
                if (codeNameBll.btnAddAll(Report))
                {
                    return "ok";
                }
                else
                {
                    return "添加失败";


                }
            }
            catch (System.Exception ex)
            {
                return "error:" + ex.Message;
            }

        }

        public string btnDeleteAll(string Report)
        {
            try
            {
                BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
                if (codeNameBll.btnDeleteAll(Report))
                {
                    return "ok";
                }
                else
                {
                    return "删除失败";


                }
            }
            catch (System.Exception ex)
            {
                return "error:" + ex.Message;
            }

        }
        public string Del(string NSRDZDAH, string Report)
        {
            BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
            Model.t_Key_NSRXX codeNameObj = new Model.t_Key_NSRXX();
            string[] str = NSRDZDAH.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<EnterpriseFund.Model.t_Key_NSRXX> modellist = new List<EnterpriseFund.Model.t_Key_NSRXX>();

            try
            {
                for (int i = 0; i < str.Length; i++)
                {
                    codeNameObj = new Model.t_Key_NSRXX();
                    codeNameObj.NSRDZDAH = Convert.ToDecimal(str[i]);
                    codeNameObj.FReportName = Report;
                   // codeNameBll.Delete(codeNameObj.NSRDZDAH, codeNameObj.FReportName);
                    modellist.Add(codeNameObj);
                }
                codeNameBll.delList(modellist);
                return "ok";
            }
            catch (Exception e)
            {
                return "error";
            }
        }
        public ActionResult ImportExcel(string Report)
        {

            ViewData["rpt"] = Report;
            return View();
        }
        public ActionResult ImportData()
        {
            BLL.EnterpriseFinanceProp efbll = new BLL.EnterpriseFinanceProp();
            BLL.EnterpriseProp enpbll = new BLL.EnterpriseProp();
            BLL.FinanceProp fanbll = new BLL.FinanceProp();
            BLL.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1BLL = new BLL.DJ_NSRXX_Extension1();
            BLL.DJ_NSRXX nsrbll = new BLL.DJ_NSRXX();
            string report = Fetch.Post("hrpt");
            var ErrMsg = "";
            try
            {
                //上传
                var uploadFiles = UploadControlExtension.GetUploadedFiles("upload");
                if (null == uploadFiles || uploadFiles.Length == 0)
                {
                    ErrMsg = "请选择Excel文件！";
                    //return ErrMsg;
                }
                var upload = uploadFiles[0];
                var clintFileName = upload.FileName;
                var extName = clintFileName.Substring(clintFileName.LastIndexOf('.') + 1);
                var svrFileName = string.Format("{0}.{1}", DateTime.Now.ToString("yyyyMMddHHmm"), extName);
                // var uploadoffiepath = string.Format("~/Publish/{0}/QTBZ/{1}{2}/", GlobalConstant.ENTITYNO, DateTime.Now.Year, DateTime.Now.Month);
                var uploadoffiepath = Server.MapPath(Url.Content("~") + string.Format("/Publish/{0}/KeyNSRImport/{1}{2}/", GlobalConstant.ENTITYNO, DateTime.Now.Year, DateTime.Now.Month));
                if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                {
                    Directory.CreateDirectory(uploadoffiepath);
                }
                upload.SaveAs(uploadoffiepath + svrFileName);

                //读取
                var dt = new BLL.SB_ZSXX().ReadExcelTitle(uploadoffiepath + svrFileName).Tables[0];

                //验证
                var cols = new List<string> { "纳税人编码", "企业名称"};
                foreach (var col in cols)
                {
                    if (!dt.Columns.Contains(col))
                    {
                        ErrMsg = "Excel文件格式错误！";
                        // return ErrMsg;
                    }
                }
                BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
                Model.t_Key_NSRXX codeNameObj = new Model.t_Key_NSRXX();
                List<string> str = new List<string>();

              
                List<Model.DJ_NSRXX_Extension1> model1List = new List<Model.DJ_NSRXX_Extension1>();
                List<Model.DJ_NSRXX_Extension1> updatelist = new List<Model.DJ_NSRXX_Extension1>();
                if (ErrMsg == "")
                {
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        var r = dt.Rows[i];
                        var excelRowIndex = i + 2;



                        Model.DJ_NSRXX n = new Model.DJ_NSRXX();

                        try
                        {
                            if (!string.IsNullOrEmpty(r["纳税人编码"].ToString()) & !string.IsNullOrEmpty(r["企业名称"].ToString()))
                            {
                                n = nsrbll.GetModelByNSRBMAndNsrmc(r["纳税人编码"].ToString(), r["企业名称"].ToString());
                                if (n == null)
                                {
                                    ErrMsg += "Excel文件第" + excelRowIndex + "行" + "纳税人信息不存在";

                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(n.NSRDZDAH.ToString()) & n.NSRDZDAH != 0)
                                    {
                                        str.Add(n.NSRDZDAH.ToString());
                                    }
                                }
                            }
                            else
                            {
                                ErrMsg += "Excel文件第" + excelRowIndex + "行" + "纳税人信息未填写完整";
                            }
                            

                        }
                        catch (System.Exception ex)
                        {
                            ErrMsg += "Excel文件第" + excelRowIndex + "行" + "纳税人信息不存在";
                     
                        }
                     
                    }
                }
                if (ErrMsg == "")
                {

                    if (str.Count > 0)
                    {
                        List<EnterpriseFund.Model.t_Key_NSRXX> modellist = new List<EnterpriseFund.Model.t_Key_NSRXX>();
                        try
                        {
                            for (int i = 0; i < str.Count; i++)
                            {
                                codeNameObj = new Model.t_Key_NSRXX();
                                codeNameObj.NSRDZDAH = Convert.ToDecimal(str[i]);
                                codeNameObj.FReportName = report;
                                modellist.Add(codeNameObj);
                            }
                            codeNameBll.AddList(modellist);

                            ErrMsg = "ok";
                        }
                        catch (Exception e)
                        {
                            ErrMsg = "error" + e.Message;
                        }
                    }
                    else
                    {
                        ErrMsg = "无匹配记录";
                    }
                    if (ErrMsg == "ok")
                    {
                        ViewData["jsAlert"] = "导入成功";
                    }
                    else
                    {
                        ViewData["jsAlert"] = ErrMsg;
                    }
                    
                    
                }
                else
                {

                    ViewData["jsAlert"] = ErrMsg;
                }
                //   upLevelAllowanceBLL.Update2(upLevelAllowanceEntryList, modela);



            }
            catch (System.Exception ex)
            {
                ViewData["jsAlert"] = "error" + ex.Message.ToString() + ErrMsg;
            }
            return View("ImportExcel");
        }

        public string DelNSRXX(string NSRDZDAH)
        {
            BLL.t_Key_NSRXX codeNameBll = new BLL.t_Key_NSRXX();
            Model.t_Key_NSRXX codeNameObj = new Model.t_Key_NSRXX();
            List<Model.t_Key_NSRXX> t_Key_NSRXX = new List<Model.t_Key_NSRXX>();
            string str = null;

            string id = Request.Params["id"];
            if (id == "1")
            {
                str = NSRDZDAH;
            }
            else
            {
                str = NSRDZDAH.Substring(0, NSRDZDAH.Length - 1);

            }
            try
            {
                if (codeNameBll.DeleteList(str))
                {
                    return "ok";
                }
                else
                    return "error";
            }
            catch
            {
                return "error";
            }
        }

    }
}
